/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.Pathology;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.PathologySyncBroker;
import systole.synchronization.remote.entities.PathologyRemote;

/**
 *
 * @author Juan Manuel
 */
public class PathologySyncBrokerDB extends BrokerDB implements PathologySyncBroker {

    public void savePathologyRemote(PathologyRemote pathologyRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving pathology remote");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(pathologyRemote);
            this.logger.logDebug("remote pathology saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save pathology remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<PathologyRemote> getPathologiesToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Pathology> getPathologiesToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting pathologies to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select p from PathologyRemote pr right join pr.pathology p where pr=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<Pathology> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get pathologies to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las patologías", e.fillInStackTrace());
        }
    }

    public Pathology getPathologyByRemoteId(int remoteId) throws ExceptionDAO {
         try {
            this.logger.logDebug("getting pathology by remote id");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    PathologyRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            PathologyRemote pathologyRemote = (PathologyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (pathologyRemote != null ? pathologyRemote.getPathology() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get pathology by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las patologías", e.fillInStackTrace());
        }
    }

    public PathologyRemote getPathologyRemoteByPathology(Pathology pathology) throws ExceptionDAO {
         try {
            this.logger.logDebug("getting pathology remote by local pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    PathologyRemote.class).add(Restrictions.eq("pathology", pathology)).setMaxResults(1);
            PathologyRemote pathologyRemote = (PathologyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return pathologyRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote pathology by pathology, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las patologías", e.fillInStackTrace());
        }
    }

    public int getLastRemotePathologyIdSynchronized() throws ExceptionDAO {
           try {
            this.logger.logDebug("getting last remote pathology sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    PathologyRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            PathologyRemote pathologyRemote = (PathologyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (pathologyRemote != null ? pathologyRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote pathology synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las patologías", e.fillInStackTrace());
        }
    }
}
